package com.dayu.finecomm.handler.tpacket;

import com.dayu.finecomm.protocol.SpecialCareChannelInboundAdapter;
import com.dayu.finecomm.protocol.pojo.TPacket;
import com.dayu.finecomm.protocol.pojo.TStation;
import com.dayu.finecomm.source.CollectSource;

import io.netty.channel.ChannelHandlerContext;

/**
 * @ClassName: TPacketCollectHandler
 * @Description:内存站点自动管理
 * @author: 戴卫国
 * @date: 2018年2月23日 下午4:11:20
 * 
 * @Copyright: 2018 dayu Inc. All rights reserved.
 */
public final class TPacketCollectHandler extends SpecialCareChannelInboundAdapter<TPacket> {

	final CollectSource source;
	final boolean udp;
	final String driveName;

	public TPacketCollectHandler(CollectSource source, boolean udp, String driveName) {

		this.source = source;
		this.udp = udp;
		this.driveName = driveName;
	}

	@Override
	public void channelRead0(ChannelHandlerContext ctx, TPacket msg) throws Exception {

		String id = msg.id();

		if (!source.collection().containsKey(id)) {

			source.collection().put(id, new TStation(msg));
		}

		TStation station = source.collection().get(id);
		station.ctx = ctx;
		station.address = msg.address;
		station.udp = udp;
		station.driveName = driveName;
		station.packet = msg;

		ctx.fireChannelRead(msg);

	}
}